import { defineStore } from 'pinia'
import { ref } from 'vue'

//用户模块 token setToken removeToken
export const useAppStore = defineStore('app', () => {
  const device = ref('desktop')
  const sidebar = ref({
    // 经验5：!!两个逻辑非操作符可以将值转换为布尔类型，+ 可以将字符串转换为数字
    opened: localStorage.getItem('sidebarStatus')
      ? !!+localStorage.getItem('sidebarStatus')!
      : true,
    withoutAnimation: false
  })

  const toggleSideBar = ({ commit }: any) => {
    commit('TOGGLE_SIDEBAR')
  }
  const closeSideBar = ({ commit }: any, { withoutAnimation }: any) => {
    commit('CLOSE_SIDEBAR', withoutAnimation)
  }
  const toggleDevice = ({ commit }: any, device: any) => {
    commit('TOGGLE_DEVICE', device)
  }

  // 定义好的仓库要暴露出去属性、方法
  return {
    device,
    sidebar,
    toggleSideBar,
    closeSideBar,
    toggleDevice
  }
})
